/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */

package tests;

import static org.junit.Assert.fail;

import java.sql.Date;
import java.util.ArrayList;

import modelLayer.Booking;
import modelLayer.Customer;

import org.junit.Test;

import dbLayer.DbBooking;
import dbLayer.DbMaxId;

public class TestDbBooking {

	DbBooking dbB = new DbBooking();
	Booking bTestObj = new Booking();

	/**
	 * Test if its possible to retrieve a booking by its ID.
	 */
	@Test
	public void testRetrieveById() {
		Booking bObj = dbB.retrieveById(1);
		if(bObj != null) {
			System.out.println("" + bObj.getBookingId());
			System.out.println("RetrieveById success");
		} else {
			fail("RetrieveById failed");
		}
	}


	/**
	 * Testing the following operations:
	 * Insert booking
	 * Update booking
	 * Delete booking
	 */
	@Test
	public void testInsertUpdateDelete() {
		bTestObj.setBookingId(DbMaxId.getMaxID("bookingId", "booking") + 1);
		Customer cObj = new Customer();
		cObj.setPartnerId(1);
		bTestObj.setCustomer(cObj);
		bTestObj.setPickUpDate(new Date(2014, 11, 11));
		bTestObj.setDeliveryDate(new Date(2014, 11, 11));
		try {
			dbB.insert(bTestObj);
			dbB.update(bTestObj);
			dbB.delete(bTestObj);
			System.out.println("InsertUpdateDelete success");
		} catch(Exception e) {
			fail("InsertUpdateDelete failed");
		}
	}

	@Test
	public void testRetrieveNonDisposed() {
		ArrayList<Booking> bList = new ArrayList<Booking>();
		bList = dbB.retrieveNonDisposed();
		if(bList.size() > 0) {
			System.out.println("RetrieveNonDisposed success. Undisposed bookings: " + bList.size());
			for(Booking b : bList) {
				System.out.println(
								b.getBookingId() +"\n"+
								b.getCustomer().getPartnerId() + "\n" +
								b.getCustomer().getBusinessName() + "\n" +
								b.getAddressList().get(0).getCity() + "\n" +
								b.getAddressList().get(1).getCity() +
								"");
			}
		} else {
			fail("RetrieveNonDisposed failed");
		}
	}

	/**
	 * Retrieve a list of all bookings and check if the size is larger than 0.
	 */	
	@Test
	public void testRetrieveAll(){
		ArrayList<Booking> list = new ArrayList<Booking>();
		try{
			list = dbB.retrieveAll();
		}catch(Exception e){
			fail("Failed to retrieve all lists.");
		}
		
		if(list.size()> 0)
			System.out.println("Successfully created list of all bookings.");
		else
			fail("Failed to create list of all bookings.");
	}

}
